import { useState, useEffect } from 'react'

const useList = () => {
    const [list, setList] = useState([]);
    const [loading, setLoading] = useState(false);
    useEffect(() => {
        setLoading(true);
        fetch('https://www.fastmock.site/mock/3c02a69b3952632e311022710cb48a6c/plant/getPlants')
            .then(res => res.json())
            .then(res => {
                setList(res.result.plantList);
                setLoading(false);
            })
    }, [])
    const collect = (id) => {
        const newList = list.map(item=>{
            if(item.plantID==id){
                return {...item,isCollect: !item.isCollect}
            }else{
                return item;
            }
        })
        setList(newList);
    }
    return [list, loading, collect]
}
export default useList;